package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.SysUser;
import com.itheima.service.UserManageService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/userManage")
public class UserManageController {

    @Reference
    UserManageService userManageService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userManageService.findPage(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> map){
        JSONObject jsonObject = (JSONObject) map.get("user");
        JSONArray jsonArray = (JSONArray)map.get("roleIds");

        SysUser user = jsonObject.toJavaObject(SysUser.class);
        Integer[] roleIds = jsonArray.toArray(new Integer[]{});

        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));

        try {
            userManageService.add(user,roleIds);
            return new Result(true, MessageConst.ADD_USER_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_USER_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,Object> map){
        JSONObject jsonObject = (JSONObject) map.get("user");
        JSONArray jsonArray = (JSONArray)map.get("roleIds");

        SysUser user = jsonObject.toJavaObject(SysUser.class);
        Integer[] roleIds = jsonArray.toArray(new Integer[]{});

        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));

        try {
            userManageService.edit(user,roleIds);
            return new Result(true, MessageConst.ADD_USER_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_USER_FAIL);
        }
    }

    @RequestMapping("/delById")
    public Result delById(Integer userId){
        try {
            userManageService.delById(userId);
            return new Result(true,MessageConst.DELETE_USER_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_USER_FAIL);
        }
    }

    @RequestMapping("/findRoles")
    public Result findRoles(Integer userId){
        try {
            Integer[] roles = userManageService.findRoles(userId);
            return new Result(true,MessageConst.QUERY_ROLE_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_ROLE_FAIL);
        }
    }
}
